package cn.edu.gdu.quizpractice.dao;

import cn.edu.gdu.quizpractice.entity.Bank;
import cn.edu.gdu.quizpractice.entity.ChoiceQuestion;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Author: Aubery
 * @Date: 2025-03-13-22:18
 * @Description:
 */
@Mapper
public interface BankDao extends BaseMapper<Bank> {
    /**
    * total + 1
    * */
    @Update("update t_banks set total = total + 1 where b_id = #{bId}")
    boolean updateTotal(Integer bId, Integer total);

    @Select("select total from t_banks where name = #{name}")
    Bank findTotalByName(String name);

    // 根据id查找
    @Select("select * from t_banks where b_id = #{bId}")
    Bank findBankById(Integer bId);

    // 只更新题库名称和是否审核
    @Update("update t_banks set name = #{name}, is_reviewed = #{isReviewed} where b_id = #{bId}")
    boolean updateByBankId(Bank bank);

    @Select("select * from t_banks")
    List<Bank> findAll();

    @Select("select * from t_choicequestions where b_name = #{name}")
    List<ChoiceQuestion> findQuestionsByBankName(String name);

    // 更新题库状态
    @Update("update t_banks set status = #{status} where b_id = #{bId}")
    boolean audit(Bank bank);

    // 获取题库数量
    @Select("select count(*) from t_banks")
    int getBankCount();

    // 获取题目总数
    @Select("select sum(total) from t_banks")
    int getTotalCount();

    @Delete("delete from t_banks where b_id = #{bId}")
    boolean deleteBankByBankId(String bId);

    @Select("select * from t_banks where is_reviewed = 1")
    List<Bank> showAuditedBanks();

    @Select("select * from t_banks where name = #{bankName}")
    Bank findBankInfoByName(String bankName);

    @Select("select * from t_banks where is_reviewed = 0")
    List<Bank> showUnAuditedBanks();

}
